function setWatcher(page, watches) {
  if (!page) return;
  let data = page.data;
  const t_watch = watches || page.watch
  let watch = typeof t_watch === 'function' ? t_watch() : t_watch;
  if (!watch) return;
  Object.keys(watch).forEach(k => {
    let key = k.split('.');
    let nowData = data;
    for (let i = 0; i < key.length - 1; i++) {
      nowData = nowData[key[i]];
    }
    let lastKey = key[key.length - 1];
    let watchFun = watch[k] && watch[k].handler || watch[k];
    let deep = watch[k].deep;
    observe(nowData, lastKey, watchFun, deep, page);
  })
}

function observe(obj, key, watchFun, deep, page) {
  var val = obj[key];
  if (deep && val != null && typeof val === 'object') {
    Object.keys(val).forEach(childKey => {
      observe(val, childKey, watchFun, deep, page);
    })
  }
  Object.defineProperty(obj, key, {
    configurable: true,
    enumerable: true,
    set: function (newVal) {
      watchFun.call(page, newVal, val);
      val = newVal;
      if (deep) {
        observe(obj, key, watchFun, deep, page);
      }
    },
    get: function () {
      return val;
    }
  })
}
export default {
  setWatcher: setWatcher
}